package top.yinjinbiao.demo.netty.custom.arom;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @Description 编码器
 * @Author yin.jinbiao
 * @Date 2021/10/8 9:53
 * @Version 1.0
 */
public class AROMEncoder extends MessageToByteEncoder<AROMDataPackage> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, AROMDataPackage aromDataPackage, ByteBuf byteBuf) throws Exception {
        // 写入消息的具体内容
        byteBuf.writeByte(aromDataPackage.getHead_data_1()); // 起始字节1
        byteBuf.writeByte(aromDataPackage.getHead_data_2()); // 起始字节2
        byteBuf.writeByte(aromDataPackage.getFrameLength()); // 帧长
        byteBuf.writeByte(aromDataPackage.getHostId());// 主机id
        byteBuf.writeByte(aromDataPackage.getCommandCode()); // 命令代码
        byteBuf.writeBytes(aromDataPackage.getParam());// 参数
        byteBuf.writeByte(aromDataPackage.getCrc()); // 数据
    }
}
